<?php
require_once APPLICATION_PATH.'/models/Webinfo.php';
require_once APPLICATION_PATH.'/models/Kefu.php';
require_once 'BaseController.php';

class WebconfController extends BaseController{

    public function baseinfoAction(){
        $tblwebinfo = new Webinfo();
        $webconf = $tblwebinfo->getWebconf();
        if($webconf==null){
            header('Location:'.ADMIN_PATH.'/Error');
            exit;
        }

        $this->view->webconf = $webconf;
    }

    public function kefuAction(){
        $tblkefu = new kefu();
        $kefu = $tblkefu->getKefu();
        if($kefu==null){
            header('Location:'.ADMIN_PATH.'/Error');
            exit;
        }

        $this->view->kefu = $kefu;
    }

    public function updatebaseAction(){
        $datatype = $this->_getParam('data');

        if($datatype == 'base'){
            //读取并设置更新字段内容
            $set= array(
                'keywords' => $this->_getParam('keywords'),
                'discription' => $this->_getParam('discription'),
                'footnav' => $this->_getParam('footnav'),
                'icpcode' => $this->_getParam('icpcode'),
                'title' => $this->_getParam('title'),
                'sectitle' => $this->_getParam('sectitle'),
            );
            $tbl = new Webinfo();
            $this->returnMessage($tbl->updateWebconf($set));
        }
        else if($datatype == 'logo'){
            $this->uploadLogoIcon('logo', 'png', 100);
        }
        else if($datatype == 'icon'){
            $this->uploadLogoIcon('favicon', 'ico', 10);
        }
        else{
            print '非法参数！';
            exit();
        }
        header('Location:'.ADMIN_PATH.'/Webconf/baseinfo');
        exit;
    }

    public function updatekefuAction(){
        $xf_effect = $this->_getParam('xf_effect')!=null?'true':'false';
        $xf_defaultopen = $this->_getParam('xf_defaultopen')!=null?'true':'false';
        $set= array(
            'xf_position' => $this->_getParam('xf_position'),
            'xf_top' => $this->_getParam('xf_top'),
            'xf_width' => $this->_getParam('xf_width'),
            'xf_effect' => $xf_effect,
            'xf_defaultopen' => $xf_defaultopen,
            'xf_qqlist' => $this->_getParam('xf_qqlist'),
            'pop_kfguin' => $this->_getParam('pop_kfguin'),
            'pop_welcome' => $this->_getParam('pop_welcome'),
        );
        $tbl = new Kefu();
        $this->returnMessage($tbl->updateKefu($set));
        header('Location:'.ADMIN_PATH.'/Webconf/kefu');
        exit;
    }

    private function returnMessage($affected){
        if($affected == 0){
            echo '更新无效：您没有对内容进行任何更改！';
        }else if($affected == 1){
            echo '更新成功！';
        }else{
            echo '更新失败！请认真检查您所填写的内容！';
        }
    }

    //上传Logo/Favicon
    private function uploadLogoIcon($filename, $ext, $size){
        $upload = new Zend_File_Transfer(); //实例化上传类
        $updir = APPLICATION_PATH.'/../public/imgs/base/';

        $upload->addValidator('Size', false, $size * 1024);  //大小限制2m
        $upload->addValidator('Extension', false, $ext);   //文件格式限制

        if (!$upload->isValid()) {
            print '文件大小或格式不符合';
            exit();
        }

        $upload->setDestination($updir);
        $upload->addFilter('Rename', array('target' => $filename.'.'.$ext, 'overwrite' => true));
        if (!$upload->receive()) {
            print '上传图片失败！';
            exit();
        }
    }

}
